<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>广告分类</span>
      <el-button
        size="mini"
        type="success"
        class="btn-add"
        @click="handleAdd"
        style="margin-left: 20px"
        >添加分类</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="adminTable"
        :data="list"
        style="width: 100%;"
        size="mini"
        v-loading="listLoading"
        border
      >
        <el-table-column label="分类ID" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="分类名称" align="center">
          <template slot-scope="scope">{{ scope.row.classifyName }}</template>
        </el-table-column>
        <el-table-column label="分类图片" width="120" align="center">
          <template slot-scope="scope">
            <div v-viewer class="img-box">
              <img
                :src="
                  scope.row.classifyPic
                    ? `${user.filePreviewUrl}/${scope.row.classifyPic}`
                    : ''
                "
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="广告名称" width="300" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.goodsName">
              {{ scope.row.goodsName }}</template
            >
            <el-button size="mini" class="danger" type="text" v-else
              >未设置</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="添加时间" width="180" align="center">
          <template slot-scope="scope">{{
            scope.row.createTime | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="排序" align="center" width="80">
          <template slot-scope="scope">{{ scope.row.classifySort }}</template>
        </el-table-column>
        <el-table-column label="操作" width="260" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdata(scope.row)"
              >编辑</el-button
            >

            <el-button
              size="mini"
              type="text"
              class="success"
              v-if="scope.row.state === 0"
              @click="handleChangeStatus(scope.row, 1)"
              >上架</el-button
            >
            <el-button
              size="mini"
              type="text"
              class="danger"
              v-else
              @click="handleChangeStatus(scope.row, 0)"
              >下架</el-button
            >
            <el-button
              size="mini"
              type="text"
              class="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="text"
              class="success"
              @click="handleSetDefault(scope.row)"
              >设置默认广告</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增/编辑 -->
    <edit-classify-dialog
      v-if="editVisible"
      :row="row"
      @handle-confirm="handleConfirm"
      @handle-cancel="editVisible = false"
    />

    <!-- 选择设置默认广告弹框 -->
    <set-goods-dialog
      v-if="goodsVisible"
      :row="row"
      @handle-confirm="handleConfirm"
      @handle-cancel="goodsVisible = false"
    />
  </div>
</template>
<script>
import { formatDate } from "@/utils/date";
import { getStorage } from "@/utils/index";
import EditClassifyDialog from "./dialogs/edit-classify-dialog.vue";
import SetGoodsDialog from "./dialogs/set-goods-dialog.vue";
import {
  getGoodsClassifyList,
  deleteGoodsClassify,
  downClassifyInfo,
  upClassifyInfo
} from "@/api/goods/index";
export default {
  name: "goodsClassify",
  components: {
    SetGoodsDialog,
    EditClassifyDialog
  },
  data() {
    return {
      listQuery: {},
      user: getStorage("user") || {},
      list: null,
      total: null,
      row: {},
      listLoading: false,
      editVisible: false,
      goodsVisible: false,
      classifyOptions: []
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    }
  },
  methods: {
    handleConfirm() {
      this.getList();
      this.editVisible = false;
      this.goodsVisible = false;
    },
    // 添加
    handleAdd() {
      this.row = {};
      this.editVisible = true;
    },
    // 编辑
    handleUpdata(row) {
      this.row = row;
      this.editVisible = true;
    },
    //更改状态
    handleChangeStatus(row, status) {
      this.$confirm(`是否要${status === 1 ? "上架" : "下架"}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        if (status === 1) {
          await upClassifyInfo({ id: row.id });
        } else {
          await downClassifyInfo({ id: row.id });
        }
        this.$message({
          type: "success",
          message: `${status === 1 ? "上架" : "下架"}成功!`
        });
        this.getList();
      });
    },
    //删除
    handleDelete(row) {
      this.$confirm("是否要删除分类?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteGoodsClassify({ id: row.id }).then(response => {
          this.$message({
            type: "success",
            message: "删除广告分类成功!"
          });
          this.getList();
        });
      });
    },

    getList() {
      this.listLoading = true;
      getGoodsClassifyList({})
        .then(response => {
          this.listLoading = false;
          this.list = response.data || [];
        })
        .catch(() => {
          this.listLoading = false;
        });
    },

    //设置默认广告
    handleSetDefault(row) {
      this.row = row;
      this.goodsVisible = true;
    }
  }
};
</script>
<style></style>
